package com.sloth.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sloth.mapper.ProductMapper;
import com.sloth.pojo.Product;
import com.sloth.pojo.ProductExample;
import com.sloth.service.ProductService;
import com.sloth.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lai
 * @time 2021/10/6 9:48
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    // 首页中的商品展示 查询
    @Override
    public ServerResponse getProductAll() {

        // 分页查询，首页只显示 36 个商品，在品牌旗舰中展示
        PageHelper.startPage(1,36);
        List<Product> productList = productMapper.selectByExample(null);
        PageInfo<Product> productPageInfo = new PageInfo<>(productList);

        return ServerResponse.getSuccess(productPageInfo);
    }

    @Override
    public ServerResponse getProductRecommendation() {

        // 分页查询，首页只显示 30 个商品，在推荐商品中展示
        PageHelper.startPage(2,30);
        List<Product> productList = productMapper.selectByExample(null);
        PageInfo<Product> productPageInfo = new PageInfo<>(productList);

        return ServerResponse.getSuccess(productPageInfo);
    }

    // 下单页面中提交订单可能提交的不是单个商品，所以向后端提交数据的时候传递的是一个包含商品id 的数据
    @Override
    public List<Product> getProductById(Integer[] productIdList) {

        ArrayList<Product> productArrayList = new ArrayList<>();

        for (Integer integer : productIdList){
            Product product = productMapper.selectByPrimaryKey(integer);
            productArrayList.add(product);
        }

        return productArrayList;
    }


    // 通过订单详情对象的商品id，查询商品价格
    @Override
    public Double getByOrderDetailsProductId(Integer productId) {

        Product product = productMapper.selectByPrimaryKey(productId);

        return product.getProPrice();
    }

    //多条件查询
    @Override
    public ServerResponse getByConditions(String proName,  String other1,String other2, Double minPrice, Double maxPrice, Double proPrice,Integer pageNum) {


        ProductExample productExample = new ProductExample();
        ProductExample.Criteria productCriteria = productExample.createCriteria();
        productExample.setOrderByClause("prod_sell DESC");
        Page page =PageHelper.startPage(pageNum, 24);
        if(proName != null && !proName.equals("-1"))
            productCriteria.andProBrandEqualTo(proName);
        if(other1 != null && !other1.equals("-1"))
            productCriteria.andOthers1EqualTo(other1);
        if(other2 != null && !other2.equals("-1"))
            productCriteria.andOthers2EqualTo(other2);
        if (minPrice != null &&minPrice != -1) {
            productCriteria.andProPriceGreaterThanOrEqualTo(minPrice);
        }
        if (maxPrice != null && maxPrice != -1) {
            productCriteria.andProPriceLessThanOrEqualTo(maxPrice);
        }

        List<Product> productList = productMapper.selectByExample(productExample);
//        System.out.println("结果1:"+productList.get(1));

        PageInfo pageInfo =   page.toPageInfo(); // pageInfo 只有页码信息
        pageInfo.setList(productList); // 重置 pageInfo数据集合 // productPageInfo(商品数据 + 页码)


        return ServerResponse.getSuccess(pageInfo);
    }
}